<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

</head>

<body>
    <div id="times"></div>
</body>
<script>
    window.onload = function () {
        //每1秒刷新时间
        setInterval("timeToDate(ntime)", 1000);
    }
    function timeToDate(time) {
        // 获取当前时间戳
        var currentTime = parseInt(new Date().getTime() / 1000);
        var diffTime = currentTime - time;
        var second = 0;
        var minute = 0;
        var hour = 0;
        if (null != diffTime && "" != diffTime) {
            if (diffTime <= 5) {
                diffTime = '刚刚';
            }
            if (diffTime < 60 && diffTime > 5) {
                diffTime = parseInt((parseFloat(diffTime / 60.0) - parseInt(diffTime / 60.0)) * 60) + "秒前";
            }
            if (diffTime > 60 && diffTime < 60 * 60) {
                diffTime = parseInt(diffTime / 60.0) + "分钟" + parseInt((parseFloat(diffTime / 60.0) - parseInt(diffTime / 60.0)) * 60) + "秒前";
            }
            else if (diffTime >= 60 * 60 && diffTime < 60 * 60 * 24) {
                diffTime = parseInt(diffTime / 3600.0) + "小时" + parseInt((parseFloat(diffTime / 3600.0) -
                    parseInt(diffTime / 3600.0)) * 60) + "分钟" +
                    parseInt((parseFloat((parseFloat(diffTime / 3600.0) - parseInt(diffTime / 3600.0)) * 60) -
                        parseInt((parseFloat(diffTime / 3600.0) - parseInt(diffTime / 3600.0)) * 60)) * 60) + "秒前";
            }
        }
        document.getElementById("times").innerHTML = diffTime;
    }
    var ntime = parseInt(new Date().getTime() / 1000);
    timeToDate(ntime);
</script>

</html>